package org.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.system.model.entity.BizTradeRecord;
import org.example.system.model.query.TradeRecordPageQuery;
import org.example.system.model.vo.PageVo;
import org.example.system.service.BizTradeRecordService;
import org.example.system.mapper.BizTradeRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【biz_trade_record(支付记录表)】的数据库操作Service实现
* @createDate 2024-11-09 19:08:01
*/
@Service
public class BizTradeRecordServiceImpl extends ServiceImpl<BizTradeRecordMapper, BizTradeRecord>
    implements BizTradeRecordService{

    @Resource
    private final BizTradeRecordMapper bizTradeRecordMapper;

    public BizTradeRecordServiceImpl(BizTradeRecordMapper bizTradeRecordMapper) {
        this.bizTradeRecordMapper = bizTradeRecordMapper;
    }

    /**
     * 根据年份查询营业额
     * @param year 年份
     * @return 营业额
     */
    @Override
    public BigDecimal selectSumByYear(Integer year) {
        LambdaQueryWrapper<BizTradeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BizTradeRecord::getTotalFee)
                .apply("YEAR(payment_time) = {0}", year)
                .groupBy(BizTradeRecord::getPaymentTime);

        List<Map<String, Object>>  maps = this.baseMapper.selectMaps(queryWrapper);
        // 判空
        if (maps == null || maps.isEmpty()) {
            return BigDecimal.ZERO;
        }
        return (BigDecimal) maps.get(0).get("totalFee");
    }

    /**
     * 根据指定的日期范围查询营业额
     * @param tradeRecordPageQuery 分页查询条件
     * @return 营业额
     */
    @Override
    public BigDecimal selectSumByDateRange(TradeRecordPageQuery tradeRecordPageQuery) {
        LambdaQueryWrapper<BizTradeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BizTradeRecord::getTotalFee)
                .ge(BizTradeRecord::getPaymentTime, tradeRecordPageQuery.getPaymentTimeStart())
                .le(BizTradeRecord::getPaymentTime, tradeRecordPageQuery.getPaymentTimeEnd());
        Map<String, Object> map = this.baseMapper.selectMaps(queryWrapper).get(0);
        return (BigDecimal) map.get("totalFee");
    }

    /**
     * 多条件分页查询交易记录
     * @param tradeRecordPageQuery 查询条件
     * @return 交易记录分页信息
     */
    @Override
    public PageVo<BizTradeRecord> searchVoPage(TradeRecordPageQuery tradeRecordPageQuery) {
        QueryWrapper<BizTradeRecord> wrapper = new QueryWrapper<>();
        // 构建查询条件
        // 1.精确匹配商户订单号查询
        wrapper.eq(
                StringUtils.isNotBlank(tradeRecordPageQuery.getTradeNo()),
                "out_trade_no",
                tradeRecordPageQuery.getTradeNo());
        // 2.精确匹配支付方式查询
        wrapper.eq(
                tradeRecordPageQuery.getPaymentType() != null && tradeRecordPageQuery.getPaymentType() > 0,
                "payment_type",
                tradeRecordPageQuery.getPaymentType());
        // 3.精确匹配支付状态查询
        wrapper.eq(
                tradeRecordPageQuery.getTradeStatus()!= null && tradeRecordPageQuery.getTradeStatus() >= -1,
                "trade_status",
                tradeRecordPageQuery.getTradeStatus());
        // 4.支付时间范围模糊查询
        wrapper.between(
                tradeRecordPageQuery.getPaymentTimeStart()!= null && tradeRecordPageQuery.getPaymentTimeEnd()!= null,
                "payment_time",
                tradeRecordPageQuery.getPaymentTimeStart(),
                tradeRecordPageQuery.getPaymentTimeEnd());
        // 5.创建时间范围模糊查询
        wrapper.between(
                tradeRecordPageQuery.getCreateTimeStart()!= null && tradeRecordPageQuery.getCreateTimeEnd()!= null,
                "create_time",
                tradeRecordPageQuery.getCreateTimeStart(),
                tradeRecordPageQuery.getCreateTimeEnd());
        System.out.println("Custom SQL Segment" + wrapper.getSqlSegment());

        // 执行查询,启动分页
        Page<BizTradeRecord> page = new Page<>(tradeRecordPageQuery.getPageNum(), tradeRecordPageQuery.getPageSize());
        IPage<BizTradeRecord> resultPage = bizTradeRecordMapper.selectVoPage(page, wrapper);

        // 构建返回结果,设置pageVo的数据
        PageVo<BizTradeRecord> pageVo = new PageVo<>();
        pageVo.setTotal(resultPage.getTotal());
        pageVo.setList(resultPage.getRecords());
        return pageVo;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void addTradeRecord(BizTradeRecord bizTradeRecord) {
        bizTradeRecordMapper.insert(bizTradeRecord);
    }
}




